package com.fan.service.bill;

import com.fan.dao.BaseDao;
import com.fan.dao.bill.BillDao;
import com.fan.dao.bill.BillDaoImpl;
import com.fan.dao.provider.ProviderDao;
import com.fan.dao.provider.ProviderDaoImpl;
import com.fan.pojo.Bill;
import com.fan.pojo.Provider;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillServiceImpl implements BillService{
    private BillDao billDao;
    public BillServiceImpl(){
        billDao = new BillDaoImpl();
    }

    @Override
    public int getBillCount(String productName, int providerId, int isPayment) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = billDao.getBillCount(connection,productName,providerId,isPayment);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    @Override
    public List<Bill> getBillList(String productName, int providerId, int isPayment, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<Bill> billList = null;
        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillList(connection,productName,providerId,isPayment,currentPageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return billList;
    }

    @Override
    public boolean addBill(Bill bill) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = billDao.addBill(connection,bill);
            connection.commit();
            if (updateRows > 0){
                flag = true;
            } else {
                System.out.println("add failed!");
            }
        } catch (Exception e){
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public boolean deleteBillById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (billDao.deleteBillById(connection,delId) > 0){
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public Bill getBillById(String id) {
        Bill bill = null;
        Connection connection =null;
        try {
            connection = BaseDao.getConnection();
            bill = billDao.getBillById(connection,id);
        }catch (Exception e){
            e.printStackTrace();
            bill = null;
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bill;
    }

    @Override
    public boolean updateBill(String id, String billCode, String productName, String productDesc, String productUnit, String productCount, String totalPrice, String isPayment, String providerId) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            if (billDao.updateBillById(connection,id,billCode,productName,productDesc,productUnit,productCount,totalPrice,isPayment,providerId)){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
}
